/* ----------------------------------------------------------------------------
* File: quiz_statistic.cpp
*
* Desc: Quiz statistic
*
* Created: 05/08/2011, by Alexey Korshunov
* ---------------------------------------------------------------------------- */
#include "quiz_statistic.h"

namespace QuizCore
{
	StatisticProvider::StatisticProvider(const UserProfileManager* manager)
		: m_UserProfileManager(const_cast<UserProfileManager*>(manager))
	{
		ReadUserStatisticInfo();
	}

	void StatisticProvider::ReadUserStatisticInfo()
	{
		const UserStatisticList& userList(m_UserProfileManager->GetUserStatisticInfo());
		for (UserStatisticList::const_iterator i = userList.begin(), ie = userList.end(); i != ie; ++i)
		{
			UserStatisticInfo info(i->User, i->Points);
			m_StatInfo.userStats.push_back(info);
		}
	}

	void StatisticProvider::IncrementCountQuestions()
	{
		++m_StatInfo.processedQuestions;
	}

	void StatisticProvider::IncrementCorrectAnswers()
	{
		++m_StatInfo.correctAnswers;
	}

	Common::Error StatisticProvider::AddPointsToUser(const std::string& User, unsigned int Points)
	{
		for (UserStatisticList::iterator it = m_StatInfo.userStats.begin(), it_end = m_StatInfo.userStats.end(); it != it_end; ++it)
		{
			if (it->User == User)
			{
				it->Points += Points;
				return Common::SKYPE_SUCCESS;
			}
		}

		UserStatisticInfo info(User, Points);
		m_StatInfo.userStats.push_back(info);
		return Common::SKYPE_SUCCESS;
	}

	const StatisticInfo& StatisticProvider::GetStatisticInfo() const
	{
		return m_StatInfo;
	}
}